package com.example.demo.controller;

import com.example.demo.entity.BuildingEntity;
import com.example.demo.entity.StudyroomEntity;
import com.example.demo.service.BuildingService;
import com.example.demo.service.StudyroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Studyroom)表控制层
 *
 * @author makejava
 * @since 2021-10-12 15:29:54
 */
@Controller
public class StudyroomController {
    /**
     * 服务对象
     */
    @Autowired
    private StudyroomService studyroomService;

    @Autowired
    private BuildingService buildingService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne1")
    public StudyroomEntity selectOne(Integer id) {
        return this.studyroomService.queryStudyroomById(id);
    }

    @RequestMapping("selectByBidStudyRoom")
    @ResponseBody
    public List<StudyroomEntity> selectByBidStudyRoom(int bid){
        return studyroomService.selectByBidStudyRoom(bid);
    }

    //展示所有自习室信息
    @RequestMapping("toShowStudyroom")
    public String toStudyroom(Model model) {
        List<StudyroomEntity> studyroomEntities = studyroomService.queryStudyroomAll();
        model.addAttribute("studyroomEntities",studyroomEntities);
        return "studyroom-show";
    }

    //前往添加自习室
    @RequestMapping("toAddStudyroom")
    public String toAddstudyroom(Model model) {
        model.addAttribute("allbuilding",buildingService.queryBuildingAll());
        return "studyroom-add";
    }

    //添加自习室
    @RequestMapping("addStudyroom")
    @ResponseBody
    public int addStudyroom(StudyroomEntity studyroomEntity) {
        return studyroomService.insertStudyroom(studyroomEntity);
    }

    //前往修改自习室
    @RequestMapping("toUpdateStudyroom")
    public String showStudyroomById(Integer sid,Model model) {
        StudyroomEntity studyroomEntity = studyroomService.queryStudyroomById(sid);
        model.addAttribute("studyroomEntity",studyroomEntity);
        return "studyroom-update";
    }

    //修改自习室信息
    @RequestMapping("updateStudyroom")
    @ResponseBody
    public int toStudyroom(StudyroomEntity studyroomEntity,Model model) {
//        studyroomEntity.setSflag();
        return studyroomService.updateStudyroom(studyroomEntity);
    }

    //自习室重名
    @RequestMapping("ifStudyroomChong")
    @ResponseBody
    public int ifStudyroomChong(String sname) {
        StudyroomEntity studyroomEntity = studyroomService.queryStudyroomByName(sname);
        if (studyroomEntity == null){
            return 1;
        }else {
            return 0;
        }
    }


    //展示所有自习室信息
    @RequestMapping("toStudyRoomSet")
    public String toStudyRoomSet(HttpServletRequest request) {
//        List<StudyroomEntity> studyroomEntities = studyroomService.queryStudyroomAll();
//        model.addAttribute("studyroomEntities",studyroomEntities);
        List<BuildingEntity> buildingEntities = buildingService.queryBuildingAll();
        request.setAttribute("buildingEntities",buildingEntities);
        return "studyRoomSet-show";
    }


}